#!python

import hashlib
import base64
import zlib
from Crypto import Random

def md5(data):
    m = hashlib.md5()
    m.update(data)
    return m.hexdigest()
    
def random_key(p):
    rand = Random.new()
    key_raw = rand.read(15)
    key = md5(key_raw + p)[5:25]
    return key, base64.b64encode(key_raw)
   
def decode_key(key_encoded,p):
    key_raw = base64.b64decode(key_encoded)
    key = md5(key_raw + p)[5:25]
    return key

def random_url():
    a=Random.new()
    l1 = 2
    l2 = 2
    ll1 = a.read(1).encode("hex")
    ll2=a.read(1).encode("hex")
    len1 = int(ll1, 16) % 10
    len2 = int(ll2, 16) % 10
    if len1:
        l1 = len1
    if len2:
        l2 = len2
    #print l1,l2
    a1 = a.read(l1).encode("hex")
    #print len(a1)
    a2 = a.read(l2).encode("hex")
    #print len(a2)
    return "/%s/%s" % (a1, a2)

class compress:
    def __init__(self, compresslevel = 9):
        self.compress = zlib.compressobj(
                compresslevel,        # level: 0-9
                zlib.DEFLATED,        # method: must be DEFLATED
                -zlib.MAX_WBITS,      # window size in bits:
                                      #   -15..-8: negate, suppress header
                                      #   8..15: normal
                                      #   16..30: subtract 16, gzip header
                zlib.DEF_MEM_LEVEL,   # mem level: 1..8/9
                0                     # strategy:
                                      #   0 = Z_DEFAULT_STRATEGY
                                      #   1 = Z_FILTERED
                                      #   2 = Z_HUFFMAN_ONLY
                                      #   3 = Z_RLE
                                      #   4 = Z_FIXED
        )
        
    def deflate(self, data):
        deflated = self.compress.compress(data)
        #deflated += self.compress.flush()
        return deflated
    def finish(self):
        return self.compress.flush()
        
class decompress:
    def __init__(self):
        self.decompress = zlib.decompressobj(-zlib.MAX_WBITS)
    def inflate(self, data):
        inflated = self.decompress.decompress(data)
        #inflated += self.decompress.flush()
        return inflated
    def finish(self):
        return self.decompress.flush()

if __name__ == "__main__":
    for i in range(20):
        url = random_url()
        print url,len(url)
